﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClinicalMassageSystem
{
    public partial class PatientOverview : Form
    {
        bool promptSave = false;

        public PatientOverview()
        {
            InitializeComponent();
        }

        CMSDataContext cms = new CMSDataContext();

        private class InjuryBinding
        {
            public Injury Injury { get; set; }
            public int InjuryID { get; set; }
            public string InjuryDisplay { get; set; }
        }

        private void PatientOverview_Load(object sender, EventArgs e)
        {
            this.CancelButton = btnCancel;
            this.btnSave.Enabled = false;

            lstCurrent.DisplayMember = "InjuryDisplay";
            lstCurrent.ValueMember = "InjuryID";

            lstCurrent.DataSource =
                from i in cms.Injuries
                where (i.isCurrentInjury == true) && i.Patient == patientBindingSource.Current
                orderby i.Date descending
                let InjuryDisplay = i.InjuryID.ToString() + ": " + i.Date.ToString() + ": " + i.Description.ToString()
                select new InjuryBinding { Injury = i, InjuryID = i.InjuryID, InjuryDisplay = InjuryDisplay };

            lstPast.DisplayMember = "InjuryDisplay";
            lstPast.ValueMember = "InjuryID";
            lstPast.DataSource =
                from i in cms.Injuries
                where (i.isCurrentInjury == false) && i.Patient == patientBindingSource.Current
                orderby i.Date descending
                let InjuryDisplay = i.InjuryID.ToString() + ": " + i.Date.ToString() + ": " + i.Description.ToString()
                select new InjuryBinding { Injury = i, InjuryID = i.InjuryID, InjuryDisplay = InjuryDisplay };

            btnSave_Click(sender, e);
            lblStatus.Text = "";
        }

        //Set patient when coming from search form
        public void setPatientBindings(int patientID)
        {
            patientBindingSource.DataSource =
                from p in cms.Patients
                where (p.PatientID == patientID)
                select p;
        }

        private void lstCurrent_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdInjuredParts.DataSource =
                from parts in cms.InjuryParts
                where parts.InjuryID == (int)lstCurrent.SelectedValue
                select new
                {
                    Still_Injured = parts.isStillInjured,
                    Part = parts.BodyPart.BodyPartName,
                    Injury_Ended = parts.DateInjuryEnded,
                    Sensitive = parts.isSensitive,
                    Pain = parts.isPain,
                    TP = parts.isTP,
                    Referral = parts.isReferrel,
                    Pain_Scale = parts.PainScale,
                    Notes = parts.Notes
                };
        }

        private void lstCurrent_DoubleClick(object sender, EventArgs e)
        {
            PatientProfileScreen patientProfileScreen = new PatientProfileScreen();
            int injuryID = (int)lstCurrent.SelectedValue;
            patientProfileScreen.setInjuryBindings(injuryID);
            //patientProfileScreen.Show();

            //PatientProfileScreen patientProfileScreen = new PatientProfileScreen();
            int patientID = int.Parse(lblPatientID.Text);
            patientProfileScreen.setPatientBindings(patientID);
            patientProfileScreen.Show();
        }

        private void lstPast_DoubleClick(object sender, EventArgs e)
        {
            PatientProfileScreen patientProfileScreen = new PatientProfileScreen();
            int injuryID = (int)lstPast.SelectedValue;
            patientProfileScreen.setInjuryBindings(injuryID);
            //patientProfileScreen.Show();

            //PatientProfileScreen patientProfileScreen = new PatientProfileScreen();
            int patientID = int.Parse(lblPatientID.Text);
            patientProfileScreen.setPatientBindings(patientID);
            patientProfileScreen.Show();
        }

        private void lstPast_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdPastInjuredParts.DataSource =
                from parts in cms.InjuryParts
                where parts.InjuryID == (int)lstPast.SelectedValue
                select new
                {
                    Still_Injured = parts.isStillInjured,
                    Part = parts.BodyPart.BodyPartName,
                    Injury_Ended = parts.DateInjuryEnded,
                    Sensitive = parts.isSensitive,
                    Pain = parts.isPain,
                    TP = parts.isTP,
                    Referral = parts.isReferrel,
                    Pain_Scale = parts.PainScale,
                    Notes = parts.Notes
                };
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Validate();
                patientBindingSource.EndEdit();
                cms.SubmitChanges();
                lblStatus.Text = "Save Successful";
                promptSave = false;
                btnSave.Enabled = false;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        private void fieldChanged(object sender, EventArgs e)
        {
            this.btnSave.Enabled = true;
            this.lblStatus.Text = "Edit in progress...";
            promptSave = true;
        }

        private void promptForSave(object sender, EventArgs e)
        {
            if (promptSave)
            {
                DialogResult q = MessageBox.Show("You have not saved your current record. Would you like to save?", "Unsaved Information", MessageBoxButtons.YesNo);
                if (q == DialogResult.Yes)
                {
                    btnSave_Click(sender, e);
                    promptSave = false;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNewInjury_Click(object sender, EventArgs e)
        {
            int patientID = int.Parse(lblPatientID.Text);
            PatientProfileScreen pps = new PatientProfileScreen();
            pps.setPatientBindings(patientID);
            pps.addInjury();
            pps.Show();
        }

        private void PatientOverview_Activated(object sender, EventArgs e)
        {
            PatientOverview_Load(sender, e);
        }

        private void PatientOverview_FormClosed(object sender, FormClosedEventArgs e)
        {
            promptForSave(sender, e);
        }
    }
}
